#!/bin/bash
########################################################################
#  NHMTools , v1.0 build 20100401
#  Function:    NHMTools script 
#  Date:        2010-06-28
#  Abstract:
########################################################################


#=======================================================================
# Define arguments for process
#=======================================================================

#-----------------------------------------------------------------------
# Process Name to display
PROC_NAME="mystope"
#-----------------------------------------------------------------------

#-----------------------------------------------------------------------
# Process Tag to identify
# Use this tag should identify the process.
# The command is :  ps -ef | grep -w $PROC_TAG
PROC_TAG="mystope-brasuser"
#-----------------------------------------------------------------------

#-----------------------------------------------------------------------
# Process Description
PROC_DESC="mystope, v1.0 build 20140522"
#-----------------------------------------------------------------------

#-----------------------------------------------------------------------
# Flags for java Virtal Machine

VM_FLAG="-DworkHome=./  -Xrs -Xms100M -Xmx512M"

#-----------------------------------------------------------------------

#-----------------------------------------------------------------------
# List of blank-separated paths defining the contents of the classes 
# and jars 
# Examples:
#       LOADER_PATH="foo foo/*.jar lib/nms-util.jar"
#     "foo": Add this folder as a class repository
#     "foo/*.jar": Add all the JARs of the specified folder as class 
#                  repositories
#     "lib/nms-util.jar": Add lib/nms-util.jar as a class repository
LOADER_PATH="./"
#-----------------------------------------------------------------------

#-----------------------------------------------------------------------
# Specify the java_home
# if it has JAVA_HOME environment argument,specify to it,
# else change it to correct java path
#-----------------------------------------------------------------------
JAVA_HOME=/jdk1.6.0_32 

#-----------------------------------------------------------------------
# Process Entrance class
MAIN_CLASS=com.eastcom.mystope.daemon.Launcher
#-----------------------------------------------------------------------

#-----------------------------------------------------------------------
# Process Arguments
PROC_ARGS=
#-----------------------------------------------------------------------

#-----------------------------------------------------------------------
# Process log file 
LOG_FILE=./log
#-----------------------------------------------------------------------


#=======================================================================
# Define information tag
#=======================================================================
RUNNING_TAG="[��]"
NOT_RUNNING_TAG="[X]"
ERROR_TAG="[E]"
INFO_TAG="[I]"

#=======================================================================
# Define functions for process
#=======================================================================
export_jre(){
	export JAVA_HOME=$JAVA_HOME
	export PATH=$JAVA_HOME/bin:$PATH
	echo $JAVA_HOME;
	echo $PATH
	export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
}
set_classpath(){
        set ${LOADER_PATH}
	
        while [ $# -gt 0 ]; do
                classpath=${classpath}:$1
                shift
        done
        CLASSPATH=mystope-server.jar
}

is_proc_run(){
        ps -ef | grep -w "${PROC_TAG}" | grep -v grep &>/dev/null
        return $?
}

status_proc(){
	export_jre
        is_proc_run
        if [ $? -eq 0 ]; then
                echo "${RUNNING_TAG} ${PROC_NAME} is running !"
                ps -ef | grep -w "${PROC_TAG}" | grep -v grep
                set_classpath
                        java -Diname=${PROC_TAG} ${VM_FLAG} -cp ${CLASSPATH} ${MAIN_CLASS} status $1 $2
        else
            echo "${NOT_RUNNING_TAG} ${PROC_NAME} is not running !"
        fi
}

start_proc(){
	export_jre
        is_proc_run
        if [ $? -eq 0 ]; then
                echo "${INFO_TAG} ${PROC_NAME} is already running !"
        else
                echo "${INFO_TAG} Starting ${PROC_NAME} ..."
                set_classpath
                nohup java -Diname=${PROC_TAG} ${VM_FLAG} -cp ${CLASSPATH} ${MAIN_CLASS} start  > ${LOG_FILE} 2>&1 &
                sleep 1
                is_proc_run
                if [ $? -eq 0 ]; then
                        echo "${INFO_TAG} ${PROC_NAME} started !"
                else
                        echo "${ERROR_TAG} ${PROC_NAME} starts failed !"
                fi

        fi
}

stop_proc(){
	export_jre
      	set_classpath
        java -Diname=${PROC_TAG} ${VM_FLAG} -cp ${CLASSPATH} ${MAIN_CLASS} stop 
}


usage(){
        echo ${PROC_DESC} usage:
        echo -e "`basename $0` <start|stop|status|restart>"
        echo -e "\tstart   - start   ${PROC_NAME}"
        echo -e "\tstop    - stop    ${PROC_NAME}"
        echo -e "\tstatus  - list    ${PROC_NAME}"
        echo -e "\trestart - restart ${PROC_NAME}"
}


#=======================================================================
# Main Program begin
#=======================================================================

case $1 in
        start)
                start_proc
                ;;
        status)
                status_proc $2 $3
                ;;
        stop)
                stop_proc
                ;;
        restart)
                stop_proc
                start_proc
                ;;
        *)
                usage
esac
